<?php

class Alerter {
	private $alerts;
	private $db_access;
	private $alerts_delete;

	public function __construct() {
		$this->alerts = array();
		$this->db_access = new DbAccessor();
	}

	public function set_db_accessor($accessor) {
		$this->db_access = $accessor;
	}

	public function get_alerts() {
		return $this->alerts;
	}

	public function get_alerts_delete() {
		return $this->alerts_delete;
	}

	/**
	 * adds an alert to the current alert array
	 */
	function add_alert($message, $link = '', $target = false, $delete = '') {
		if ($target) {
			$sqlizedMessage = $this->db_access->db_sqlize($message);
			$query = $this->db_access->db_query_insert(array('user_id', 'alert'), 
													   array($target, $sqlizedMessage), "ALERT");
			$this->db_access->db_do_query($query);
		} else {
			array_push($this->alerts, array('message' => $message, 'link' => $link, 'delete' => $delete));
			if ($delete) {
				if ($this->alerts_delete) {
					$this->alerts_delete .= ",{$delete}";
				} else {
					$this->alerts_delete .= $delete;
				}
			}
		}
	}

	function db_catch($error, $mysqlerror) {
		$this->add_alert($mysqlerror);
		throw new Exception($error); //TODO: remove Exception and make it a more specific DbException
	}
}

?>
